package org.jypj.dev.controller;

import org.jypj.dev.dao.IGiveWorkDao;
import org.jypj.dev.dao.IRoadDao;
import org.jypj.dev.dao.TrainNumInfoDao;
import org.jypj.dev.model.*;
import org.jypj.dev.service.*;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.PropertyUtil;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by chenyuyuyu on 2017/6/4.
 */
@Controller
@RequestMapping("webApp")
public class WebAppController extends BaseController {

    @Resource
    private ILoginInfoService loginInfoService;
    @Resource
    private ITemplateService templateService;
    @Resource
    private IStationWeatherService stationWeatherService;
    @Resource
    private TrainNumInfoDao trainNumInfoDao;

    @Resource
    private IGiveWorkDao giveWorkDao;

    @Resource
    private IRoadDao roadDao;

    @Resource
    private INoticeMessageService noticeMessageService;

    @Resource
    private IUserService userService;

    @Resource
    private NoticeReadUserService noticeReadUserService;

    /**
     * 显示消息内容
     *
     * @param msgId
     * @param request
     * @return
     */
    @RequestMapping(value = "showMsg/{msgId}/{userNum}")
    public String toShowMessage(@PathVariable String msgId, @PathVariable String userNum, HttpServletRequest request) {
        String fileServerUrl = PropertyUtil.getProperties("file_server_url");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            NoticeReadUser noticeReadUser = new NoticeReadUser();
            String lowUUID = StringUtil.getLowUUID();
            noticeReadUser.setId(lowUUID);
            noticeReadUser.setNoticeId(msgId);
            noticeReadUser.setUserNum(userNum);
            noticeReadUser.setReadTime(sdf.format(new Date()));
            noticeReadUserService.saveNoticeReadUser(noticeReadUser);
        } catch (Exception e) {
            e.printStackTrace();
        }
        NoticeMessage noticeMessage = noticeMessageService.selectNoticeMessageById(msgId);
        request.setAttribute("nm", noticeMessage);
        request.setAttribute("fileServerUrl", fileServerUrl);
        return "webApp/message_show";
    }

    /**
     * 显示详情
     * loginInfoService
     *
     * @return
     */
    @RequestMapping(value = "show/{loginId}")
    public String toTrainNoticeAppShow(HttpServletRequest request, @PathVariable String loginId) {
        LoginInfo loginInfo = loginInfoService.selectLoginInfoById(loginId);
        Map<String, Object> queryMap = new HashMap<>();
        List<StationWeather> stationWeathers = new ArrayList<>();
        if (loginInfo != null) {
            String roadId = loginInfo.getRoadId();
            queryMap.put("roadId", roadId);
            queryMap.put("queryDate", DateUtils.getTodayString("yyyy-MM-dd HH"));
            stationWeathers = stationWeatherService.selectAllByMap(queryMap);
        }
        List<Template> templates = templateService.selectAllByMap(queryMap);
        request.setAttribute("ts", templates);
        request.setAttribute("li", loginInfo);
        request.setAttribute("sws", stationWeathers);
        return "webApp/train_notice_app_show";
    }

    /**
     * 跳转到出乘提示设置页面
     *
     * @return
     */
    @RequestMapping(value = "tnInfo/{trainNum}")
    public String toTrainNumInfo(@PathVariable String trainNum, HttpServletRequest request) {
        TrainNumInfo trainNumInfo = trainNumInfoDao.selectTrainNumInfoById(trainNum);
        if (trainNumInfo != null) {
            request.setAttribute("tn", trainNumInfo);
        } else {
            return "webApp/error_info";
        }
        return "webApp/train_num_info";
    }

    /**
     * 显示派班信息
     *
     * @param userNum
     * @param workDate
     * @return
     */
    @RequestMapping(value = "giveWorkInfo/{userNum}/{workDate}")
    public String toGiveWorkInfo(@PathVariable String userNum, @PathVariable String workDate, HttpServletRequest request) {
        //派班信息显示
        Map<String, Object> map = new HashMap<>();
        map.put("userNum", userNum);
        map.put("workDate", workDate);
        GiveWork giveWork = giveWorkDao.selectObjectByMap(map);
        request.setAttribute("giveWork", giveWork);
        if (giveWork != null) {
            Road road = roadDao.selectRoadById(giveWork.getRoadId());
            request.setAttribute("road", road);
            request.setAttribute("train", road.getTrains().get(0));
        }
        return "webApp/give_work_info";
    }


    /**
     * 查询用户基本信息
     *
     * @param userNum
     * @param request
     * @return
     */
    @RequestMapping(value = "showUserInfo/{userNum}")
    public String showUserInfo(@PathVariable String userNum, HttpServletRequest request) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("userNum", userNum);
        LoginInfo loginInfo = loginInfoService.selectObjectByMap(queryMap);
        User user = userService.selectUserByUserNum(userNum);
        request.setAttribute("loginInfo", loginInfo);
        request.setAttribute("user", user);
        return "webApp/user_info";
    }


    /**
     * 查看帮助文档
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "appHelpShow")
    public String appHelpShow(HttpServletRequest request) {
        request.setAttribute("content", getSetting().getApp_help());
        return "webApp/app_help_show";
    }
}


